/*
* PROJECT: Happy New Year
* VERSION: 1.0
* AUTHOR: Mattias Thell
* URL: http://www.mattiasthell.com
*
* */

#include "Menu.hpp"
#include "World.hpp"
#include "HnyMenuItem.hpp"
#include "Settings.hpp"

#include <sstream>

namespace hny {

	Menu::Menu(World* world)
		:mWorld(world), mDisplayScore(false)
	{
		int w = Settings::GetInstance()->SystemScreenWidth;
		int h = Settings::GetInstance()->SystemScreenHeight;

		int w2 = w/2;

		mFont = new hgeFont("Content/font1.fnt");

		mCursor = new hgeSprite(mWorld->GetTexture("cursor"), 0,0,32,32);

		mGui = new hgeGUI();

		mGui->AddCtrl(new HnyMenuItem(1, mFont, (float)w2, 300.0f, "New Game"));
		mGui->AddCtrl(new HnyMenuItem(2, mFont, (float)w2, 350.0f, "High Scores"));
		mGui->AddCtrl(new HnyMenuItem(3, mFont, (float)w2, 400.0f, "Quit"));

		mGui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		
		mGui->SetFocus(1);
		mGui->Enter();

		mScoreGui = new hgeGUI();
		mScoreGui->AddCtrl(new HnyMenuItem(1, mFont, (float)w2, 500.0f, "Back"));
		mScoreGui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
		mScoreGui->SetFocus(1);
		
	}

	Menu::~Menu()
	{
		//delete mFont;
		//delete mGui;
	}

	bool Menu::Update(float dt) {
		
		if(mDisplayScore) {

			int sid = mScoreGui->Update(dt);

			if(sid == 1) {
				mDisplayScore = false;
				mGui->Enter();
				mGui->SetFocus(1);
			}
		}
		else
		{
			int id = mGui->Update(dt);

			switch(id)
			{
			case 1:
				mWorld->StartNewGame();
				break;
			case 2:
				mScoreGui->SetFocus(1);
				mScoreGui->Enter();
				mDisplayScore = true;
				break;
			case 3: 
				return true;
			}
		}
		
		

		return false;
		
	}

	void Menu::Draw() {
		if(mDisplayScore) {
			
			hgeColor c = mFont->GetColor();

			float sx = 450.0f;
			float sy = 300.0f;
			float row = 40.0f;

			ScoreVector scores = mWorld->GetHighScores();

			int place = 1;

			ScoreVector::iterator i;
			for(i = scores.begin(); i != scores.end(); i++) {

				ScoreEntry entry = (*i);

				std::stringstream ss;
				ss << place << ". " << entry.Score;

				if(entry.New)
					mFont->SetColor(hgeColor(1.0f, 0.2f, 0.2f, 1.0f).GetHWColor());
				else
					mFont->SetColor(hgeColor(0.4f, 0.4f, 1.0f, 1.0f).GetHWColor());

				mFont->Render(sx, sy, HGETEXT_LEFT, ss.str().c_str());
				sy += row;
				place++;

				if(place > 5)
					break;
			}

			mFont->SetColor(c.GetHWColor());
			mScoreGui->Render();
		}
		else {
			mGui->Render();
		}
	}

	void Menu::DisplayScore(bool enable)
	{
		mDisplayScore = enable;
	}
}
